<template>
  <div class="app-container">
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['manager:assess:add']"
        >新增
        </el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="assessList" @selection-change="handleSelectionChange">
      <el-table-column label="满意度名称" align="center" prop="normDescribe"/>
      <el-table-column label="星级" align="center" prop="startLevel"/>
      <el-table-column label="是否显示列表" align="center" prop="itemText" :formatter="sfShowList"/>
      <el-table-column label="是否显示评价文本框" align="center" prop="textBox" :formatter="sfShowAppraise"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['manager:assess:edit']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['manager:assess:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改评价设置对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="150px">
        <el-form-item label="满意度名称" prop="normDescribe">
          <el-input v-model="form.normDescribe" placeholder="请输入满意度名称"/>
        </el-form-item>

        <el-form-item label="评分星级" prop="startLevel">
          <div style="margin-top: 8px">
            <el-rate v-model="form.startLevel" :allow-half="true"></el-rate>
          </div>
        </el-form-item>

        <el-form-item label="评价内容" prop="normCode">
          <el-select v-model="form.normCodeArray" multiple placeholder="请选择评价内容" style="width: 100%">
            <el-option
              v-for="content in contentOptions"
              :key="content.label"
              :label="content.value"
              :value="content.label"
            ></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="选择事项" prop="sourceFlag">
          <el-select v-model="form.sourceFlag" placeholder="请选择选择事项" style="width: 100%">
          </el-select>
        </el-form-item>
        <el-form-item label="是否显示评价内容" prop="itemText">
          <el-switch v-model="form.itemText"/>
        </el-form-item>
        <el-form-item label="是否显示评价文本框" prop="textBox">
          <el-switch v-model="form.textBox"/>
        </el-form-item>
        <el-form-item label="是否进行强制评价" prop="isforce">
          <el-switch v-model="form.isforce"/>
        </el-form-item>
        <el-form-item label="序号" prop="sort">
          <el-input v-model="form.sort" placeholder="请输入排序号码"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {listAssess, getAssess, delAssess, addAssess, updateAssess, selectOptions} from "@/api/manager/assess";

export default {
  name: "Assess",
  dicts: ['sys_user_sex', 'sys_show_hide'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 评价设置表格数据
      assessList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        sort: [
          {required: true, message: "排序号码不能为空", trigger: "blur"}
        ],
      },
      // 评价内容下拉选项
      contentOptions: [],
      normCodeArray: []
    };
  },
  created() {
    this.getList();
    this.selectOptions();
  },
  methods: {
    /** 查询评价设置列表 */
    getList() {
      this.loading = true;
      listAssess(this.queryParams).then(response => {
        this.assessList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 查询评价内容下拉选择 */
    selectOptions() {
      selectOptions().then(response => {
        this.contentOptions = response.data
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        normCode: null,
        sourceFlag: null,
        normName: null,
        normDescribe: null,
        startLevel: null,
        itemText: null,
        textBox: null,
        type: null,
        uuid: null,
        sort: null,
        isforce: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.uuid)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加评价设置";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const uuid = row.uuid || this.ids
      getAssess(uuid).then(response => {
        this.form = response.data;
        this.form.itemText = this.form.itemText == "1" ? true : false;
        this.form.textBox = this.form.textBox == "1" ? true : false;
        this.form.isforce = this.form.isforce == "1" ? true : false;

        if (this.form.normCode && this.form.normCode != null && this.form.normCode != "null") {
          this.$set(this.form, "normCodeArray", this.form.normCode.split(","));
        }

        this.form.startLevel = parseFloat(this.form.startLevel);

        this.open = true;
        this.title = "修改评价设置";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.form.normCode = this.form.normCodeArray.join(",");
          this.form.itemText = this.form.itemText ? 1 : 0;
          this.form.textBox = this.form.textBox ? 1 : 0;
          this.form.isforce = this.form.isforce ? 1 : 0;
          if (this.form.uuid != null) {
            updateAssess(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addAssess(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const uuids = row.uuid || this.ids;
      this.$modal.confirm('是否确认删除评价设置编号为"' + uuids + '"的数据项？').then(function () {
        return delAssess(uuids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('manager/assess/export', {
        ...this.queryParams
      }, `assess_${new Date().getTime()}.xlsx`)
    },
    /**  是否显示列表 */
    sfShowList(row) {
      return row.itemText == "1" ? "是" : "否";
    },
    /**  是否显示评价文本框 */
    sfShowAppraise(row) {
      return row.textBox == "1" ? "是" : "否";
    }
  }
};
</script>
